<?php
//error_reporting(E_ALL);
class booklist_controller_index extends SnsController {
	
	var  $data = '';
	public function __construct($app, $controller){
		parent::__construct($app, $controller);
		$this->data = new booklist_model_data();
	}
	
	/**
	 * 添加书单
	 */
	public function addBookList_action(){
		$this->requireLogin();
		$this->_renderJson();
		
		$args['title']	=	_('title');
		$args['uid']	=	self::$login['uid'];
		
		if($rs = $this->data->add($args)){
			if($rs<0){
				switch($rs){
					case -1:
						$this->ret = self::failure('参数错误!');
						break;
					case -2:
						$this->ret = self::failure('书单名称已经存在');
						break;
					case -3:
						$this->ret = self::failure('你的书单数量已经超过最大限制');
						break;
				}
			}
			else{
				$this->ret = self::success($rs);
			}
		}else{
			$this->ret = self::failure('未知错误');
		}
	}
	/**
	 * 修改书单名
	 */
	public function edit_action(){
		$this->requireLogin();
		$this->_renderJson();
		$args['id'] = _('blid');
		$args['title'] = _('title');
		if($this->data->edit($args)){
			$this->ret = self::success();
		}else{
			$this->ret	=	self::failure('修改失败');
		}
	}
	
	/**
	 * 删除书单
	 */
	public function delBookList_action(){
		$this->requireLogin();
		$this->_renderJson();
		$blid = _('id');
		if($this->data->delBookList($blid,self::$login['uid'])){
			$this->ret = self::success('删除成功！');
		}else{
			$this->ret	=	self::failure('删除失败！');
		}
	}
	
	
	/**
	 * 添加关系
	 */
	public function addRel_action(){
		$this->requireLogin();
		$this->_renderJson();
		
		$blid	=	_('blid');
		$cid	=	_('cid');
		$uid	=	self::$login['uid'];
		$rs = $this->data->addRel($cid,$uid,$blid);
		if($rs === true){
			$this->ret = self::success();
		}else{
			if($rs == Error::ERROR_WRONG_PARAMS){
				$this->ret = self::failure('参数错误！');
			}
			elseif($rs == Error::ERROR_RECORD_EXISTS ){
				$this->ret = self::failure('您已经添加到您的书单了!');
			}
			else {
				$this->ret = self::failure('未知错误');
			}
		}
	}
	
	/**
	 * 删除关系
	 */
	public function delRel_action(){
		$this->requireLogin();
		$this->_renderJson();
		$cid	=	_('cid');
		$blid	=	_('blid');
		$uid	=	self::$login['uid'];
		
		if($this->data->removeRel($cid,$blid,$uid)){
			$this->ret = self::success('删除成功！');
		}else{
			$this->ret = self::failure('删除失败！');
		}
	}
	/**
	 * 获取我的书单列表
	 */
	public function myBookList_action(){
		$this->requireLogin();
		$this->_renderJson();
		if($list = $this->data->getBookList(self::$login['uid'])){
			$this->ret = self::success($list);
		}else{
			$this->ret = self::failure('暂无数据');
		}
	}
	
	/**
	 * 书签的漫画列表
	 */
	public function mylist_action(){
		$this->requireLogin();
		$blid	=	_('blid');
		$uid	=	self::$login['uid'];
		$page	=	_('page',1);
		$total = 0;
		if($blist = $this->data->getBookList($uid)){
			foreach($blist as $row){
				$total+=$row['nums'];
			}
		}
		$list	=	$this->data->getList($uid,$blid,$page,16);
		$num = $this->data->getDefaultCount($uid);
		
		$this->ret = array('data' => $list['ROWS'],'blist'=>$blist,'total'=>$total+$num,'default_num'=>$num,'pages'=>$this->genPages($list['INFO'],array('blid'=>$blid)));
		$this->view = 'booklist';
	}
	
	/**
	 * 获取我好友的
	 *
	 */
	public function frilist_action(){
		$this->requireLogin();
		$frids = friends_util_cache::getFriendIds(self::$login['uid']);
		$list = $this->data->getFriBlist($frids);
	}
}